package com.api.aiyh_kafang.dao;

import aiyh.utils.Util;
import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl;
import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_kafang.entity.UfInvoiceConfigDTO;
import weaver.conn.RecordSet;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.List;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/11/9 0009 14:38
 */


public class InvoiceMapping {
	private RecordSet rs = new RecordSet();
	private ToolUtil toolUtil = new ToolUtil();
	private final String ModeTable = "uf_fpyzjb";
	private final String ModeDetailTable = "uf_fpyzjb_dt1";

	public UfInvoiceConfigDTO getConfigInfo(String workflowId) {
		String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
		String query = "select ic.id,ic.invoice_browse,ic.workflow_nodes,ic.workflow_type, " +
				"dtv.tablename invoice_detail_table,ftv.fieldname invoice_field,ftv1.fieldname invoice_price " +
				"from uf_invoice_config ic " +
				"left join workflow_detail_table_view dtv on ic.invoice_detail_table = dtv.id " +
				"left join workflow_field_table_view ftv on ic.invoice_field = ftv.id " +
				"left join workflow_field_table_view ftv1 on ic.invoice_price = ftv1.id " +
				" where workflow_type in (" + workflowId + " )";
		rs.executeQuery(query);
		UfInvoiceConfigDTO ufInvoiceConfigDTO = Util.recordeSet2Entity(rs, UfInvoiceConfigDTO.class, true);
		if (ufInvoiceConfigDTO != null) {
			ufInvoiceConfigDTO.setWorkflowType(versionStringByWfid);
		}
		return ufInvoiceConfigDTO;
	}

	public void saveInvoiceInfo(int dataId, String tableName, String requestId) {
//		主表数据
		Map<String, Object> update = Util.createUtilHashMap()
				.uPut("lc", requestId)
				.uPut("sm", "发票扫描自动添加实体发票信息！");
		Where where = Util.createPrepWhereImpl().whereAnd("id").whereEqual(dataId);
		PrepSqlResultImpl sqlResult = Util.createSqlBuilder().updateSql(tableName, update, where);
		toolUtil.writeErrorLog("SQL：" + sqlResult.getSqlStr() + " ---->参数：" + sqlResult.getArgs());
		rs.executeUpdate(sqlResult.getSqlStr(), sqlResult.getArgs());
	}

	public void saveInvoiceDetail(Map<String, Object> insert, String modeTable) {
		PrepSqlResultImpl sqlResult = Util.createSqlBuilder().insertSql(modeTable, insert);
		toolUtil.writeErrorLog("SQL：" + sqlResult.getSqlStr() + " ---->参数：" + sqlResult.getArgs());
		rs.executeUpdate(sqlResult.getSqlStr(), sqlResult.getArgs());
	}

	public String getMainId(String requestId) {
		String query = "select * from uf_fpyzjb where lc = ?";
		rs.executeQuery(query, requestId);
		rs.next();
		return rs.getString("id");
	}

	public List<Map<String, Object>> selectModeInfo(String mainId) {
		RecordSet rs = new RecordSet();
		String query = "select * from " + ModeDetailTable + " where mainid = ?";
		rs.executeQuery(query, mainId);
		return Util.recordSet2MapList(rs);
	}


	public List<Map<String, Object>> selectWorkflowData(String workflowId, String requestId, UfInvoiceConfigDTO configInfo) {
//		根据流程id查询流程主表
		String mainTable = Util.getMainTable(workflowId);
		String query = "select * from " + mainTable + "  where requestid = ?";
		rs.executeQuery(query, requestId);
		rs.next();
		String mainId = rs.getString("id");
//		查询明细表数据
		RecordSet rs = new RecordSet();
		query = "select * from " + configInfo.getInvoiceDetailTable() + " where mainid = ?";
		rs.executeQuery(query, mainId);
		return Util.recordSet2MapList(rs);
	}

	public String getWorkflowCode(String workflowCodeId, String invoiceTable) {
		String query = "select * from " + invoiceTable + " where id = ?";
		rs.executeQuery(query, workflowCodeId);
		if (rs.next()) {
			return rs.getString("invoicenumber");
		} else {
			throw new RuntimeException("发票不存在！");
		}
	}

	public boolean selectExist(String main, String number) {
		rs.executeQuery("select * from " + ModeDetailTable + " where mainid = ? and fphm2 = ?",main,number);
		return rs.next();
	}
}